package com.itc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itc.system.api.domain.SysMenu;
import com.itc.system.domain.SysDeptMenu;
import com.itc.system.mapper.SysDeptMenuMapper;
import com.itc.system.service.SysDeptMenuService;
import com.itc.system.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author wangjinxia
* @description 针对表【sys_dept_menu】的数据库操作Service实现
* @createDate 2024-05-11 16:38:36
*/
@Service
public class SysDeptMenuServiceImpl extends ServiceImpl<SysDeptMenuMapper, SysDeptMenu>
    implements SysDeptMenuService{

    @Autowired
    @Lazy
    private SysMenuService sysMenuService;

    @Override
    public List<SysDeptMenu> getDeptMenuByDeptId(Long deptId) {
        List<SysDeptMenu> deptMenuByDeptId = this.baseMapper.getDeptMenuByDeptId(deptId);
        return deptMenuByDeptId;
    }

    @Override
    public List<String> getMenuPermissionByDeptId(Long menuDeptId) {
        List<String> perms = new LinkedList<>();
        List<SysDeptMenu> deptMenus = this.baseMapper.selectList(
                new LambdaQueryWrapper<SysDeptMenu>()
                .eq(SysDeptMenu::getDeptId,menuDeptId)
        );
        if(deptMenus.size() == 0){
            return perms;
        }
        List<Long> menuIds = deptMenus.stream().map(SysDeptMenu::getMenuId).collect(Collectors.toList());
        List<SysMenu> menuList = sysMenuService.getBaseMapper().selectList(
                new LambdaQueryWrapper<SysMenu>()
                .in(SysMenu::getMenuId,menuIds)
                .isNotNull(SysMenu::getPerms)
        );
        if(menuList.size() == 0){
            return perms;
        }
        perms = menuList.stream().map(SysMenu::getPerms).collect(Collectors.toList());
        return perms;
    }
}




